package com.ebraga.trackerclub.android.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import com.ebraga.trackerclub.android.exception.BackofficeUtilException;

public class DateUtils {
	
	public final static String DATE_DEFAULT_PATTERN = "dd/MM/yyyy";
	public final static String DATE_TIME_DEFAULT_PATTERN = "dd/MM/yyyy HH:mm";
	
	public static Date addMinutes(Date data, int minutes) {

		Calendar c = Calendar.getInstance();

		c.setTime(data);

		c.add(Calendar.MINUTE, minutes);

		return c.getTime();

	}

	public static Date addDays(Date data, int days) {

		Calendar c = Calendar.getInstance();

		c.setTime(data);

		c.add(Calendar.DAY_OF_MONTH, days);

		return c.getTime();

	}
	
	/**
	 * Method responsible to calculate the difference between two dates
	 * 
	 * @param dateStart
	 * @param dateEnd
	 * @param typeReturn
	 * @return
	 * @throws BackofficeUtilException
	 */
	public static long diffBetweenDate(Date dateStart, Date dateEnd, int typeReturn) {
	
			//in milliseconds
			long diff = dateEnd.getTime() - dateStart.getTime();
 
			long diffReturn = 0;
			
			//Diferença em minutos
			if (typeReturn == Calendar.MINUTE) {
				
				diffReturn = diff / (60 * 1000);
			
			//Diferença em segundos
			} else if (typeReturn == Calendar.SECOND) {
				
				diffReturn =  diff / 1000;
				
				//Diferença em horas
			} else if (typeReturn == Calendar.HOUR) {
				
				diffReturn = diff / (60 * 60 * 1000);
				
				//Diferença em dias
			} else if (typeReturn == Calendar.DATE) {
				
				diffReturn = diff / (24 * 60 * 60 * 1000);
				
			}
 
			return diffReturn;		
	}	
	
	/**
	 * Method responsible to return complete difference between two dates.
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long[] getTimeDifference(Date d1, Date d2) {
		
		long[] result = new long[5];
		Calendar cal = Calendar.getInstance();
		cal.setTimeZone(TimeZone.getTimeZone("UTC-3:00"));
		cal.setTime(d1);

		long t1 = cal.getTimeInMillis();
		cal.setTime(d2);

		long diff = Math.abs(cal.getTimeInMillis() - t1);
		final int ONE_DAY = 1000 * 60 * 60 * 24;
		final int ONE_HOUR = ONE_DAY / 24;
		final int ONE_MINUTE = ONE_HOUR / 60;
		final int ONE_SECOND = ONE_MINUTE / 60;

		long d = diff / ONE_DAY;
		diff %= ONE_DAY;

		long h = diff / ONE_HOUR;
		diff %= ONE_HOUR;

		long m = diff / ONE_MINUTE;
		diff %= ONE_MINUTE;

		long s = diff / ONE_SECOND;
		long ms = diff % ONE_SECOND;
		result[0] = d;
		result[1] = h;
		result[2] = m;
		result[3] = s;
		result[4] = ms;

		return result;
	}	
	
	/**
	 * Method responsible to return complete difference between two dates.
	 * @param d1
	 * @param d2
	 * @param dayLikeMinute
	 * @return
	 */
	
	public static long[] getTimeDifference(Date d1, Date d2, boolean dayLikeMinute) {
		
		long[] result = getTimeDifference(d1, d2);
		if (dayLikeMinute) {
			long day = result[0];
			result[0] = 0;
			long minute = (day * 24);
			result[1] = result[1] + minute;
		}
		
		return result;
	}
	
	public static Date addDays(Date data, int days, boolean zerarHora) {
		
		Calendar c = Calendar.getInstance();
		
		c.setTime(data);
		
		c.add(Calendar.DAY_OF_MONTH, days);
		
		if ( zerarHora ) {
			
			c.add( Calendar.HOUR , 0);
			c.add( Calendar.MINUTE , 0);
			c.add( Calendar.SECOND , 0);
			c.add( Calendar.MILLISECOND , 0);
		}
		
		return c.getTime();
	}

	public static String format(Date date, String format) {
		SimpleDateFormat DATE_DEFAULT_FORMATTER = new SimpleDateFormat(format);
		return date!=null ? DATE_DEFAULT_FORMATTER.format(date) : null;
	}		

	public static String formatDefault(Date date) {
		SimpleDateFormat DATE_DEFAULT_FORMATTER = new SimpleDateFormat(DATE_TIME_DEFAULT_PATTERN);
		return date!=null ? DATE_DEFAULT_FORMATTER.format(date) : null;
	}	
	
	
}
